import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.ByteBuffer;

import java.util.ArrayList;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class VideoSummarize {

	private static VideoFileReader videoReader = null;
	private static ByteBuffer frame;
	private static MyFileWriter writer = null;

	final static int img_width = 320;
	final static int img_height = 240;
	final static int img_bpp = 3;

	/**
	 * @param args
	 * @throws LineUnavailableException 
	 * @throws IOException 
	 * @throws UnsupportedAudioFileException 
	 */
	public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
		if (args.length != 3) {
			err("Usage: VideoSummarize <rgb file name> <wav file name> <fraction>");
			return;
		}

		try {
			videoReader = new VideoFileReader(args[0]);
		} catch (FileNotFoundException e) {
			err("Input files not found");
			return;
		}
		
		FileWriter fW = null;
		BufferedWriter wr = null; 
		
		fW = new FileWriter("c:\\users\\srikanth\\dump.txt");
		wr = new BufferedWriter(fW);




		try {
			writer = new MyFileWriter(img_width, img_height, img_bpp, args[0],
					args[0] + ".out");
		} catch (FileNotFoundException e1) {
			err("Unable to create output file" + e1.getMessage());
			return;
		}

		frame = ByteBuffer.allocateDirect(img_width * img_height * img_bpp);

		HistogramAnalyzer hist = new HistogramAnalyzer(img_width, img_height);
		MVAnalyzer mv = new MVAnalyzer(img_width, img_height);
		
		AudioFileReader audioReader = new AudioFileReader(args[1]);

		FileStats stats = new FileStats();

		int frameID = 0;
		//int start_writing = 100;
		while (true) {
		//for( int a =0; a < 1000; a++) {
			try {
				int res = videoReader.getNextFrame(frame);

				if (-1 == res) {
					info("File completed.");
					break;
				}
			} catch (IOException e) {
				err(e.toString());
				break;
			}
			double histoDiff = hist.getScore(frame);
			double motion    = mv.getScore(frame);
			stats.insertData(frameID, motion, histoDiff);

			info("FrameID: " + frameID + " Hist: " + histoDiff + " MV: " + motion);

			frameID++;

			frame.clear();

		}
		
		try {
			videoReader.close();
		} catch (IOException e) {
			err(e.toString());
			return;
		}
		
		audioReader.readAndCalcStats(stats);
		
//		for(int i = 0; i < stats.frameInfoList.size(); i++) {
//			frameInfo f = stats.frameInfoList.get(i);
//			info(f.frameID + ": " + f.audio + "," + f.histogramDifference + "," + f.motion + "," + f.score);
//		}
		
		try {
			writer.openInputFile();
		} catch (FileNotFoundException e) {
			err(e.toString());
			return;
		}

		double fraction = Double.parseDouble(args[2]);
		ArrayList<VideoRange> interesting = new ArrayList<VideoRange>();
		
		stats.getInterestingSequences(fraction, interesting);
		
		//wr.write(Integer.toString(interesting.size())); wr.newLine();
		for (int i = 0; i < interesting.size(); i++) {
			VideoRange vr = interesting.get(i);
			vr.startFrameID = (vr.startFrameID/4)*4;
			vr.stopFrameID = (vr.stopFrameID/4)*4;
			info("Seq = " + vr.startFrameID + "-"
					+ vr.stopFrameID +"(" + (vr.stopFrameID - vr.startFrameID) + ")");
			
//			wr.write(Integer.toString(vr.startFrameID)); wr.newLine();
//			wr.write(Integer.toString(vr.stopFrameID)); wr.newLine();
//			
//			wr.flush();
			try {
				writer.writeRange(interesting.get(i));
			} catch (IOException e) {
				err(e.toString());
				return;
			}
		}
		
		
		
		
	}

	private static void err(String input) {
		System.out.println("ERROR >> " + input);
	}

	private static void info(String input) {
		System.out.println("Info  >> " + input);
	}

}
